package com.company.project.rocketmq.processor;

import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.company.project.constant.TradeEnums;
import com.company.project.model.TradeOrder;
import com.company.project.protocol.mq.PaidMQ;
import com.company.project.rocketmq.IMessageProcessor;
import com.company.project.service.TradeOrderService;

/**
 * 收到 支付成功的消息 将订单状态改为已支付
 */
@Component
public class PaidProcessor implements IMessageProcessor {
	public static final Logger LOGGER = LoggerFactory.getLogger(PaidProcessor.class);
	@Autowired
	private TradeOrderService orderService;

	public boolean handleMessage(MessageExt messageExt) {
		try {
			String body = new String(messageExt.getBody(), "UTF-8");
			String msgId = messageExt.getMsgId();
			String tags = messageExt.getTags();
			String keys = messageExt.getKeys();
			LOGGER.info(" PaidProcessor receive message:" + messageExt);
			PaidMQ paidMQ = JSON.parseObject(body, PaidMQ.class);

			TradeOrder record = new TradeOrder();
			record.setOrderId(paidMQ.getOrderId());
			record.setPayStatus(TradeEnums.PayStatusEnum.PAID.getStatusCode());
			orderService.update(record);
			return true;
		} catch (Exception e) {
			return false;
		}

	}

}
